//using System.Windows.Input;
	//using System.Windows.Data;
		//using System.Windows.Documents;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using Microsoft.VisualBasic;
using System;
//using System.Windows.Media.Imaging;
//using System.Windows.Media;
//using System.Windows.Shapes;
//using System.Windows.Controls;


	public class DocumentEditor : RichTextBox
	{
		
		
		public bool FileChanged = false;
		public string DocumentName = null;
		public double ZoomLevel = 1;
		
		public DocumentEditor()
		{
			CaretPosition.Paragraph.LineHeight = 1;
			AcceptsTab = true;
			Margin = new Thickness(-2, -3, 0, 0);
		}
		
#region LoadDocument
		
		public void LoadDocument(string filename)
		{
			try
			{
				System.IO.FileInfo f = new System.IO.FileInfo(filename);
				System.IO.FileStream fs = System.IO.File.Open(f.FullName, System.IO.FileMode.Open);
				TextRange tr = new TextRange(Document.ContentStart, Document.ContentEnd);
				DocumentTab p = (DocumentTab)Parent;
				if (f.Extension == ".xaml")
				{
					FlowDocument content = System.Windows.Markup.XamlReader.Load(fs) as FlowDocument;
					Document = content;
				}
				else if (f.Extension == ".html" || f.Extension == ".htm")
				{
					fs.Close();
					fs = null;
					try
					{
						//Dim content As FlowDocument = TryCast(Markup.XamlReader.Parse(HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(My.Computer.FileSystem.ReadAllText(filename), True)), FlowDocument)
						//Document = content hainh
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message, "Error loading html document");
					}
				}
				else if (f.Extension == ".rtf")
				{
					tr.Load(fs, System.Windows.DataFormats.Rtf);
				}
				else
				{
					tr.Load(fs, System.Windows.DataFormats.Text);
				}
				if (fs != null)
				{
					fs.Close();
					fs = null;
				}
				DocumentName = filename;
				p.SetDocumentTitle(f.Name);
				FileChanged = false;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Error loading document");
			}
		}
		
#endregion
		
#region SaveDocument
		
		public void SaveDocument(string filename)
		{
			System.IO.FileInfo file = new System.IO.FileInfo(filename);
			System.IO.FileStream fs = System.IO.File.Open(filename, System.IO.FileMode.OpenOrCreate);
			TextRange tr = new TextRange(Document.ContentStart, Document.ContentEnd);
			DocumentTab p = (DocumentTab)Parent;
			if (file.Extension == ".xaml")
			{
				System.Windows.Markup.XamlWriter.Save(Document, fs);
			}
			else if (file.Extension == ".rtf")
			{
				tr.Save(fs, System.Windows.DataFormats.Rtf);
			}
			else if (file.Extension == ".txt")
			{
				tr.Save(fs, System.Windows.DataFormats.Text);
			}
			else
			{
				tr.Save(fs, System.Windows.DataFormats.Text);
			}
			fs.Close();
			p.SetDocumentTitle(file.Name);
			DocumentName = filename;
			FileChanged = false;
		}
		
#endregion
		
		private void DocumentEditor_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (FileChanged == false)
			{
				FileChanged = true;
			}
		}
		
#region Find
		
		public TextRange FindWordFromPosition(System.Windows.Documents.TextPointer position, string word)
		{
			while (position != null)
			{
				if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
				{
					string textRun = position.GetTextInRun(LogicalDirection.Forward);
					// Find the starting index of any substring that matches "word".
					int indexInRun = textRun.IndexOf(word);
					if (indexInRun >= 0)
					{
						TextPointer start = position.GetPositionAtOffset(indexInRun);
						TextPointer end = start.GetPositionAtOffset(word.Length);
						return new TextRange(start, end);
					}
				}
				position = position.GetNextContextPosition(LogicalDirection.Forward);
			}
			// position will be null if "word" is not found.
			return null;
		}
		
#endregion
		
#region Subscript/Superscript
		
		public void ToggleSubscript()
		{
			var currentAlignment = this.Selection.GetPropertyValue(Inline.BaselineAlignmentProperty);
			BaselineAlignment newAlignment = (((BaselineAlignment) currentAlignment) == BaselineAlignment.Subscript) ? BaselineAlignment.Baseline : BaselineAlignment.Subscript;
			this.Selection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, newAlignment);
		}
		
		public void ToggleSuperscript()
		{
			var currentAlignment = this.Selection.GetPropertyValue(Inline.BaselineAlignmentProperty);
			BaselineAlignment newAlignment = (((BaselineAlignment) currentAlignment) == BaselineAlignment.Superscript) ? BaselineAlignment.Baseline : BaselineAlignment.Superscript;
			this.Selection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, newAlignment);
		}
		
#endregion
		
#region Strikethrough
		
		public void ToggleStrikethrough()
		{
			TextRange range = new TextRange(Selection.Start, Selection.End);
			TextDecorationCollection t = (TextDecorationCollection) (Selection.GetPropertyValue(Inline.TextDecorationsProperty));
			if (t == null || ! t.Equals(TextDecorations.Strikethrough))
			{
				t = TextDecorations.Strikethrough;
			}
			else
			{
				t = new TextDecorationCollection();
			}
			range.ApplyPropertyValue(Inline.TextDecorationsProperty, t);
		}
		
#endregion
		
//#region GetWordCount
		
//        public int GetWordCount()
//        {
//            int SpacePos;
//            int X = 1;
//            int WordCount = 0;
//            bool NoMore = false;
//            int CharValue;
//            TextRange tr = new TextRange(Document.ContentStart, Document.ContentEnd);
//            string content = tr.Text;
//            content = content.Replace(Constants.vbCr, " ");
//            content = content.Replace(Constants.vbLf, " ");
//            if (content.Trim().Length > 0)
//            {
//                while (NoMore == false)
//                {
//                    SpacePos = (content.Trim()).IndexOf(" ", X);
//                    if (SpacePos > 0)
//                    {
//                        CharValue = Strings.Asc(content.Substring(X - 1, 1));
//                        if (CharValue > 64 && CharValue < 91 || CharValue > 96 && CharValue < 123 || CharValue > 47 && CharValue < 58)
//                        {
//                            WordCount++;
//                        }
//                        X = SpacePos + 1;
//                        //string a = content.Substring(X - 1, 1);
//                        while (content.Substring(X - 1, 1)==null)
//                        {
//                            X++;
//                        }
//                    }
//                    else
//                    {
//                        if (X <= content.Length)
//                        {
//                            CharValue = Strings.Asc(content.Substring(X - 1, 1));
//                            if (CharValue > 64 && CharValue < 91 || CharValue > 96 && CharValue < 123 || CharValue > 47 && CharValue < 58)
//                            {
//                                WordCount++;
//                            }
//                        }
//                        NoMore = true;
//                    }
//                }
//            }
//            return WordCount;
//        }
		
//#endregion
		
#region GoToLine
		
		public void GoToLine(int linenumber)
		{
			TextPointer ls = Document.ContentStart.DocumentStart.GetLineStartPosition(0);
			TextPointer p = Document.ContentStart.GetLineStartPosition(0);
			int @int = 2;
			while (true)
			{
				int r;
				p = p.GetLineStartPosition(1, out r);
				if (r == 0)
				{
					this.CaretPosition = p;
					break;
				}
				if (linenumber == @int)
				{
					this.CaretPosition = p;
					break;
				}
				@int++;
			}
		}
		
#endregion
		
	}

